package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /*@Autowired
    private CategoryService categoryService;*/
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //添加套餐的基本信息
        this.save(setmealDto);
        //添加对应套餐下的菜品
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithSetmealDish(List<Long> ids) {
        //获取到所有要删除的套餐对象
        List<Setmeal> setmeals = this.listByIds(ids);
        log.info("setmeals:{}", setmeals);
        for (Setmeal setmeal : setmeals) {
            if (setmeal.getStatus() == 1) {
                throw new CustomException("当前套餐包含售卖状态套餐，如需删除请先停售");
            }
        }
        //如果没有是售卖中的 ，可以直接删除
        //先删除套餐基本信息
        this.removeByIds(ids);
        //删除套餐菜品关系表
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);

    }

    //回显要修改套餐的编辑框
    @Override
    public SetmealDto getWithSetmealDish(Long id) {
        //查询基本信息
        Setmeal setmeal = this.getById(id);
        //创建要返回的对象
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //查询套餐下的菜品信息（查询套餐菜品关系表）
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        //给setmealDto的list属性赋值
        setmealDto.setSetmealDishes(list);
        /*//查询套餐对应的分类名称
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String name = category.getName();
        //给套餐下的分类名称赋值
        setmealDto.setCategoryName(name);*/
        return setmealDto;

    }

    //修改套餐信息
    @Override
    public void updateWithSetmeal(SetmealDto setmealDto) {
        //修改套餐的基本信息
        this.updateById(setmealDto);
        //修改套餐关系表
        //套餐关系表是一个集合不能直接修改，先删除，后添加
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(setmealDto.getId() != null, SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        //再向套餐关系表中添加数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //因为关系表中得关联套餐id页面是不存在设置值得，需要手动设置
        //获取套餐id
        Long setmealId = setmealDto.getId();
        //遍历关系表集合
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());


        setmealDishService.saveBatch(setmealDishes);
    }
}
